package com.cyx.controller;

import com.cyx.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/json")
public class JsonController {

    @RequestMapping(value = "/user",produces = "application/json;charset=utf-8")
    @ResponseBody   //加了此注解不再经过视图解析器处理，直接返回字符串，注意中文乱码处理
    public String getUser() throws JsonProcessingException {
        //注意返回对象会显示中文乱码,需要设置produces
        User user = new User("发斯蒂",23);
//        return user.toString();

        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }

    @RequestMapping("/user1")
    @ResponseBody
    public String getUser1() throws JsonProcessingException {
        User user = new User("给第三方",23);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }

}
